/**
 * Utility functions for handling classifier nodes (categories/else cases).
 */

/**
 * Generate the list of cases for a classifier node.
 * @param {object} node - The node object, expecting node.properties.options.categories and/or node.properties.options.else.
 * @returns {Array} Array of case descriptor objects ({type, label, value}).
 */
function getCaseList(node) {
  const cases = [];
  const options = node?.properties?.options ?? {};

  // Add categories as CASE
  if (Array.isArray(options.categories)) {
    options.categories.forEach((category, idx) => {
      cases.push({
        type: "CASE",
        label: `分类 ${idx + 1}`,
        value: category,
      });
    });
  }

  // Add else as ELSE
  if (options.else) {
    cases.push({
      type: "ELSE",
      label: "ELSE",
      value: options.else,
    });
  }

  return cases;
}

/**
 * Generate an anchor ID for a classifier node.
 * @param {string} nodeId - The classifier node ID.
 * @param {string} type - "CASE" or "ELSE".
 * @param {number} index - The 1-based case index.
 * @returns {string} The anchor ID.
 */
function getAnchorId(nodeId, type, index) {
  const suffix = `case_${type === "ELSE" ? "else" : index}`;
  return `${nodeId}_${suffix}`;
}

/**
 * Get anchorId by category index.
 * @param {string} nodeId - The classifier node ID.
 * @param {number} chooseIndex - -1 for ELSE, otherwise CASE index.
 * @returns {string} The anchor ID.
 */
function getAnchorIdByChooseIndex(nodeId, chooseIndex) {
  const type = chooseIndex === -1 ? "ELSE" : "CASE";
  // Index is 1-based for CASEs
  return getAnchorId(nodeId, type, chooseIndex + 1);
}

const ClassifierHelpers = Object.freeze({
  getAnchorId,
  getAnchorIdByChooseIndex,
  getCaseList,
  [Symbol.toStringTag]: "Module",
});

export { ClassifierHelpers as C };